{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import win32gui as win # 操作windows窗口\n",
    "from PIL import ImageGrab, Image\n",
    "import PIL # 操作图像\n",
    "import win32con # 系统操作\n",
    "import time\n",
    "# import datetime\n",
    "from datetime import datetime, timedelta\n",
    "import win32api\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\fansila\\AppData\\Local\\YunZen\\TimeLog C:\\Users\\fansila\\AppData\\Local\\YunZen\\TimeLog\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "import platform, platformdirs\n",
    "import os\n",
    "base_path = platformdirs.user_data_dir('TimeLog', 'YunZen')\n",
    "capture_path = os.path.join(base_path, 'capture')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "sleep_second = 1\n",
    "capture_interval_second = 10\n",
    "remain_second_to_capture = 0\n",
    "capture_save_path = './output/'\n",
    "\n",
    "titles = []\n",
    "start_times = []\n",
    "end_times = []\n",
    "\n",
    "\n",
    "while True:\n",
    "    remain_second_to_capture -= 1\n",
    "\n",
    "    time.sleep(sleep_second)\n",
    "\n",
    "    # 获取最前活动窗口的标题\n",
    "    title = win.GetWindowText(win.GetForegroundWindow())\n",
    "    # win.Get\n",
    "\n",
    "    # 我需要下面那么复杂的截图判断逻辑吗？\n",
    "    # if title == '': # 桌面\n",
    "\n",
    "    now = datetime.now()\n",
    "\n",
    "    if len(titles) == 0 or title != titles[-1] or remain_second_to_capture <= 0:\n",
    "        # 标题变化或不剩余时间了，截图\n",
    "        img = ImageGrab.grab()\n",
    "        img.save(capture_save_path + now.strftime('%Y_%m_%d_%H_%M_%S_%f') + '.png')\n",
    "        remain_second_to_capture = capture_interval_second\n",
    "\n",
    "    if len(titles) == 0 or title != titles[-1]:\n",
    "        # 标题变化了，记录\n",
    "        titles.append(title)\n",
    "        start_times.append(now)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['● tmp.ipynb - timelog - Visual Studio Code', 'tmp.ipynb - timelog - Visual Studio Code', '新标签页 和另外 20 个页面 - Fansila - Microsoft\\u200b Edge', 'Python 学习笔记之—— PIL 库 - 知乎 和另外 20 个页面 - Fansila - Microsoft\\u200b Edge', '(4条消息) Python 格式化输出时间（包含毫秒）_王20133的博客-CSDN博客_python 时间格式化毫秒 和另外 19 个页面 - Fansila - Microsoft\\u200b Edge', 'datetime strftime 毫秒 - 搜索 和另外 18 个页面 - Fansila - Microsoft\\u200b Edge', 'Python笔记_第二篇_面向过程_第二部分_4.常用模块的简单使用_窗体控制模块(win32con、win32gui) - 时海涛|Thomas - 博客园 和另外 17 个页面 - Fansila - Microsoft\\u200b Edge', '新标签页 和另外 16 个页面 - Fansila - Microsoft\\u200b Edge', 'python win32gui 自动化操作 - 简书 和另外 16 个页面 - Fansila - Microsoft\\u200b Edge', '新标签页 和另外 15 个页面 - Fansila - Microsoft\\u200b Edge', 'python 实现超快窗口截图，自动获取当前活动窗口并展示截图 - 知乎 和另外 15 个页面 - Fansila - Microsoft\\u200b Edge', '新标签页 和另外 14 个页面 - Fansila - Microsoft\\u200b Edge', '(1 封私信) 首页 - 知乎 和另外 14 个页面 - Fansila - Microsoft\\u200b Edge', '', 'ManicTime - 专业版（试用）', '', '(1 封私信) 首页 - 知乎', 'ManicTime - 专业版（试用）', '', '(1 封私信) 首页 - 知乎', 'tmp.ipynb - timelog - Visual Studio Code']\n",
      "[datetime.datetime(2023, 2, 4, 23, 40, 12, 185961), datetime.datetime(2023, 2, 4, 23, 40, 13, 341322), datetime.datetime(2023, 2, 4, 23, 40, 20, 493710), datetime.datetime(2023, 2, 4, 23, 40, 22, 358607), datetime.datetime(2023, 2, 4, 23, 40, 45, 269398), datetime.datetime(2023, 2, 4, 23, 40, 52, 573631), datetime.datetime(2023, 2, 4, 23, 40, 55, 880083), datetime.datetime(2023, 2, 4, 23, 41, 8, 875542), datetime.datetime(2023, 2, 4, 23, 41, 11, 90891), datetime.datetime(2023, 2, 4, 23, 41, 14, 332653), datetime.datetime(2023, 2, 4, 23, 41, 16, 620860), datetime.datetime(2023, 2, 4, 23, 41, 19, 879203), datetime.datetime(2023, 2, 4, 23, 41, 22, 135526), datetime.datetime(2023, 2, 4, 23, 41, 23, 435397), datetime.datetime(2023, 2, 4, 23, 41, 25, 635741), datetime.datetime(2023, 2, 4, 23, 41, 26, 831625), datetime.datetime(2023, 2, 4, 23, 41, 27, 990310), datetime.datetime(2023, 2, 4, 23, 41, 30, 157908), datetime.datetime(2023, 2, 4, 23, 41, 31, 346502), datetime.datetime(2023, 2, 4, 23, 41, 32, 532065), datetime.datetime(2023, 2, 4, 23, 41, 34, 714479)]\n"
     ]
    }
   ],
   "source": [
    "print(titles)\n",
    "print(start_times)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1575268\n",
      "python 判断两个字符串的相似度的两个方法 - 简书 和另外 3 个页面 - Fansila - Microsoft​ Edge\n",
      "-335475275\n",
      "(2, 3, (-21333, -21333), (-1, -1), (469, 41, 1063, 559))\n",
      "(-7, -7, 1714, 1044)\n",
      "Chrome_WidgetWin_1\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "time.sleep(2)\n",
    "\n",
    "foreground_window = win.GetForegroundWindow()\n",
    "print(foreground_window)\n",
    "\n",
    "print(win.GetWindowText(foreground_window))\n",
    "# print(win.GetWindowLong(foreground_window))\n",
    "print(win.GetWindowDC(foreground_window))\n",
    "# print(win.GetWindowExtEx(foreground_window))\n",
    "# print(win.GetWindowOrgEx(foreground_window))\n",
    "print(win.GetWindowPlacement(foreground_window))\n",
    "print(win.GetWindowRect(foreground_window))\n",
    "# print(win.GetWindowRgn(foreground_window))\n",
    "\n",
    "# c = win.GetCapture()\n",
    "print(win.GetClassName(foreground_window))\n",
    "print(win.GetClassLong(foreground_window, 0))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Code.exe\n"
     ]
    }
   ],
   "source": [
    "import win32process\n",
    "import win32gui\n",
    "import psutil\n",
    "# 获取最前活动窗口的应用程序名\n",
    "window = win32gui.GetForegroundWindow()\n",
    "pid = win32process.GetWindowThreadProcessId(window) # 获取窗口线程ID+进程ID\n",
    "name = psutil.Process(pid[-1]).name() # 获取进程名\n",
    "print(name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数据库打开成功\n"
     ]
    }
   ],
   "source": [
    "import sqlite3\n",
    "\n",
    "conn = sqlite3.connect('db/test.db')\n",
    "\n",
    "print (\"数据库打开成功\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.4.39\n"
     ]
    }
   ],
   "source": [
    "from flask_sqlalchemy import SQLAlchemy\n",
    "import flask_sqlalchemy\n",
    "import sqlalchemy\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "DLL load failed while importing win32ui: 找不到指定的程序。",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_216364\\1810457690.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mwin32ui\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mwin32ui\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__doc__\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mImportError\u001b[0m: DLL load failed while importing win32ui: 找不到指定的程序。"
     ]
    }
   ],
   "source": [
    "import win32ui\n",
    "\n",
    "win32ui.__doc__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ImportError",
     "evalue": "DLL load failed while importing win32api: 找不到指定的程序。",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_19996\\3364283643.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mpywinauto\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;31m# 使用pywinauto获取Chrome窗口对象\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[0mchrome_app\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpywinauto\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mApplication\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbackend\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"uia\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconnect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtitle_re\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\".*Google Chrome\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0mchrome_window\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mchrome_app\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwindow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtitle_re\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\".*Google Chrome\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mc:\\dev\\anaconda3\\lib\\site-packages\\pywinauto\\__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     57\u001b[0m     \u001b[1;31m#     ImportError: No system module 'pywintypes' (pywintypes27.dll)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     58\u001b[0m     \u001b[1;31m# So try to facilitate pywintypes*.dll loading with implicit import of win32api\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 59\u001b[1;33m     \u001b[1;32mimport\u001b[0m \u001b[0mwin32api\u001b[0m  \u001b[1;31m# noqa: E402\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     60\u001b[0m     \u001b[1;32mimport\u001b[0m \u001b[0mpythoncom\u001b[0m  \u001b[1;31m# noqa: E402\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     61\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mImportError\u001b[0m: DLL load failed while importing win32api: 找不到指定的程序。"
     ]
    }
   ],
   "source": [
    "import pywinauto\n",
    "\n",
    "# 使用pywinauto获取Chrome窗口对象\n",
    "chrome_app = pywinauto.Application(backend=\"uia\").connect(title_re=\".*Google Chrome\")\n",
    "chrome_window = chrome_app.window(title_re=\".*Google Chrome\")\n",
    "\n",
    "# 获取当前活动标签页的URL\n",
    "tab = chrome_window.child_window(auto_id=\"1\", control_type=\"Tab\")\n",
    "url = tab.children()[1].get_value()\n",
    "\n",
    "print(url)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b63c7080fdb1d92464456dfca5fac8661fb477c698f200a66cd30a96dbf5149d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
